1
|
|
|
import axios from "axios" |
2
|
|
|
import React, { ChangeEvent, FormEvent } from "react" |
3
|
|
|
import { useEffect, useState } from "react" |
4
|
|
|
import { Link } from "gatsby" |
5
|
|
|
import { SearchResult } from "../Types/Search" |
6
|
|
|
import "./SearchContainer.scss" |
7
|
|
|
|
8
|
|
|
const SearchContainer = () => { |
9
|
|
|
const [searchResults, setSearchResults] = useState<SearchResult[]>([]) |
10
|
|
|
const [loading, setLoading] = useState<boolean>(true) |
11
|
|
|
const [error, setError] = useState<boolean>(false) |
12
|
|
|
const [searchQuery, setSearchQuery] = useState<string>(``) |
13
|
|
|
|
14
|
|
|
const queryResults = searchQuery === `` ? [] : searchResults |
15
|
|
|
|
16
|
|
|
const handleSubmit = (e: FormEvent) => { |
17
|
|
|
e.preventDefault() |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
useEffect(() => { |
21
|
|
|
async function searchData() { |
22
|
|
|
if (searchQuery !== null) { |
23
|
|
|
const response = await axios.get( |
24
|
|
|
`https://api.kcvvelewijt.be/jsonapi/index/kcvv_content?filter[fulltext]=${searchQuery}` |
25
|
|
|
) |
26
|
|
|
setSearchResults(response.data.data) |
27
|
|
|
setLoading(false) |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
searchData().catch((error) => { |
31
|
|
|
setError(true) |
32
|
|
|
console.log(`====================================`) |
33
|
|
|
console.log(`Something bad happened while fetching some data for the search.\n${error}`) |
34
|
|
|
console.log(`====================================`) |
35
|
|
|
}) |
36
|
|
|
}, [searchQuery]) |
37
|
|
|
|
38
|
|
|
const searchData = (e: ChangeEvent) => { |
39
|
|
|
setSearchQuery((e.target as HTMLInputElement).value) |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
const renderForm = (searchQuery: string) => ( |
43
|
|
|
<form onSubmit={handleSubmit}> |
44
|
|
|
<div> |
45
|
|
|
<label className={`search_input__label`} htmlFor="search"> |
46
|
|
|
<input |
47
|
|
|
id="search" |
48
|
|
|
onChange={searchData} |
49
|
|
|
className={`search_input__input`} |
50
|
|
|
placeholder="Spelersnaam, ploegnaam, deel van een artikel..." |
51
|
|
|
value={searchQuery} |
52
|
|
|
required |
53
|
|
|
/> |
54
|
|
|
<span className={`search_input__label__inner_wrapper`}> |
55
|
|
|
<span className={`search_input__label__inner_text`}>Waar bent u naar op zoek?</span> |
56
|
|
|
</span> |
57
|
|
|
</label> |
58
|
|
|
</div> |
59
|
|
|
</form> |
60
|
|
|
) |
61
|
|
|
|
62
|
|
|
const renderQueryResults = (queryResults: SearchResult[]) => ( |
63
|
|
|
<div> |
64
|
|
|
<h3>Gevonden resultaten: {queryResults.length}</h3> |
65
|
|
|
<table> |
66
|
|
|
<thead> |
67
|
|
|
<tr> |
68
|
|
|
<th>Titel</th> |
69
|
|
|
</tr> |
70
|
|
|
</thead> |
71
|
|
|
<tbody> |
72
|
|
|
{queryResults.map((item) => { |
73
|
|
|
return renderQueryResultItem(item) |
74
|
|
|
})} |
75
|
|
|
</tbody> |
76
|
|
|
{renderQueryResultsCaption()} |
77
|
|
|
</table> |
78
|
|
|
</div> |
79
|
|
|
) |
80
|
|
|
|
81
|
|
|
const renderQueryResultsCaption = () => ( |
82
|
|
|
<caption> |
83
|
|
|
<i className={`article__footer_related__icon article__footer_related__icon--node--article fa`} /> Nieuwsbericht |
84
|
|
|
<br /> |
85
|
|
|
<i className={`article__footer_related__icon article__footer_related__icon--node--team fa`} /> Ploeg |
86
|
|
|
<br /> |
87
|
|
|
<i className={`article__footer_related__icon article__footer_related__icon--node--player fa`} /> Speler |
88
|
|
|
<br /> |
89
|
|
|
<i className={`article__footer_related__icon article__footer_related__icon--node--staff fa`} /> Staflid |
90
|
|
|
<br /> |
91
|
|
|
<i className={`article__footer_related__icon article__footer_related__icon--node--event fa`} /> Evenement |
92
|
|
|
<br /> |
93
|
|
|
</caption> |
94
|
|
|
) |
95
|
|
|
|
96
|
|
|
const renderQueryResultItem = (item: SearchResult) => ( |
97
|
|
|
<tr key={`row_${item.attributes.drupal_internal__nid}`}> |
98
|
|
|
<td> |
99
|
|
|
<Link to={item.attributes.path.alias}> |
100
|
|
|
<i className={`article__footer_related__icon article__footer_related__icon--${item.type} fa`} /> |
101
|
|
|
{item.attributes.title} |
102
|
|
|
</Link> |
103
|
|
|
</td> |
104
|
|
|
</tr> |
105
|
|
|
) |
106
|
|
|
|
107
|
|
|
if (loading) { |
108
|
|
|
return <p>Zoekfunctie is aan het laden...</p> |
109
|
|
|
} |
110
|
|
|
if (error) { |
111
|
|
|
return ( |
112
|
|
|
<> |
113
|
|
|
<h2>Aiiii...</h2> |
114
|
|
|
<h3> |
115
|
|
|
Er ging iets mis - gelieve <a href="mailto:[email protected]">contact op te nemen</a> indien het probleem |
116
|
|
|
zich blijft voordoen. |
117
|
|
|
</h3> |
118
|
|
|
</> |
119
|
|
|
) |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return ( |
123
|
|
|
<div className="search__wrapper"> |
124
|
|
|
{renderForm(searchQuery)} |
125
|
|
|
{queryResults.length > 0 && renderQueryResults(queryResults)} |
126
|
|
|
</div> |
127
|
|
|
) |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
export default SearchContainer |
131
|
|
|
|